package com.egg.mall.service.spu.impl;

import cn.hutool.core.util.ObjectUtil;
import com.egg.common.core.constant.StringPool;
import com.egg.common.mybatis.service.impl.BaseServiceImpl;
import com.egg.common.util.JsonUtil;
import com.egg.mall.entity.spu.Spu;
import com.egg.mall.mapper.spu.SpuMapper;
import com.egg.mall.model.request.SpuPostOrPut;
import com.egg.mall.model.request.SpuStatePut;
import com.egg.mall.service.sku.ISkuService;
import com.egg.mall.service.spu.ISpuAttributeService;
import com.egg.mall.service.spu.ISpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author east
 * @since 2022-08-08
 */
@Slf4j
@Service
public class SpuServiceImpl extends BaseServiceImpl<SpuMapper, Spu> implements ISpuService {


    @Autowired
    private ISpuAttributeService spuAttributeService;
    @Autowired
    private ISkuService skuService;

    /**
     * 新增SPU
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Long addSpu(SpuPostOrPut param) {
        Spu spu = JsonUtil.objToObj(param, Spu.class);
        if (ObjectUtil.isNotEmpty(param.getSubPicUrls())) {
            spu.setAlbum(param.getSubPicUrls().stream().collect(Collectors.joining(StringPool.COMMA)));
        }
        baseSave(spu);
        Long spuId = spu.getId();
        // 属性
        param.getAttrList().forEach(o -> o.setId(null));
        spuAttributeService.saveAttr(spuId, param.getAttrList());
        // 规格
        Map<String, Long> specIdMap = spuAttributeService.saveSpec(spuId, param.getSpecList());
        // sku
        skuService.saveSku(spuId, param.getSkuList(), specIdMap);
        return spuId;
    }

    /**
     * 修改SPU
     *
     * @param id
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Long editSpu(Serializable id, SpuPostOrPut param) {
        Spu existSpu = baseGetById(id);
        Spu spu = JsonUtil.objToObj(param, Spu.class);
        spu.setId(existSpu.getId());
        baseEdit(spu);
        Long spuId = spu.getId();
        // 属性
        param.getAttrList().forEach(o -> o.setId(null));
        spuAttributeService.saveAttr(spuId, param.getAttrList());
        // 规格
        Map<String, Long> specIdMap = spuAttributeService.saveSpec(spuId, param.getSpecList());
        // sku
        skuService.saveSku(spuId, param.getSkuList(), specIdMap);
        return spuId;
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delSpu(Collection<? extends Serializable> ids) {
        baseDel(ids);
        skuService.delBySpuId(ids);
        spuAttributeService.delBySpuId(ids);
    }

    /**
     * 修改状态
     *
     * @param id
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Long editSpuState(Serializable id, SpuStatePut param) {
        Spu existSpu = baseGetById(id);
        if (Objects.equals(existSpu.getState(), param.getState())) {
            return existSpu.getId();
        }
        Spu spu = new Spu();
        spu.setId(existSpu.getId());
        spu.setState(param.getState());
        baseEdit(spu);
        return spu.getId();
    }


}
